package com.yh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.yh.dao.UserDao;
import com.yh.model.UserModel;
import com.yh.util.MyBatisUtil;

public class UserDaoImpl implements UserDao{

	public List<UserModel> getAll() {
		SqlSession session = MyBatisUtil.getSession();
		List<UserModel> userList=new ArrayList<UserModel>();
		try {
			userList=session.selectList("com.yh.dao.UserDao.getAll");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSession();
		}
		return userList;
	}

	public int deleteUser(Integer id) {
		SqlSession session = MyBatisUtil.getSession();
		int a = 0;
		try {
			a = session.delete("com.yh.dao.UserDao.deleteUser", id);
			session.commit();
		} finally {
			MyBatisUtil.closeSession();
		}		
		return a;
	
	}
	
	public int addUser(UserModel user) {
		SqlSession session = MyBatisUtil.getSession();
		int a = 0;
		try {
			a = session.insert("com.yh.dao.UserDao.addUser", user);
			session.commit();
		} finally {
			MyBatisUtil.closeSession();
		}		
		return a;
	}

	public UserModel selectUser(String name) {
		UserModel user=null;
		SqlSession session = MyBatisUtil.getSession();
		try {
			user = session.selectOne("com.yh.dao.UserDao.selectUser", name);
		} finally {
			MyBatisUtil.closeSession();
		}		
		return user;
	}

	public int updateUser(UserModel user) {
		SqlSession session = MyBatisUtil.getSession();
		int a = 0;
		try {
			System.out.println("执行SQL"+user.getName());
			a = session.update("com.yh.dao.UserDao.updateUser", user);
			session.commit();
		} finally {
			MyBatisUtil.closeSession();
		}		
		return a;
	
	}

	public UserModel findUserById(Integer id) {
		UserModel user=null;
		SqlSession session = MyBatisUtil.getSession();
		try {
			user = session.selectOne("com.yh.dao.UserDao.findUserById",id);
		} finally {
			MyBatisUtil.closeSession();
		}		
		return user;
	}

	
}
